package game;

import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player extends FlyingObj implements Constants 
{
    private final int INIT_Y = MAIN_HEIGHT - 100; 
    private final int INIT_X = MAIN_WIDTH / 2;
    private String player = "/image/player.png";
    
    public int missileState; //1: normal missiles; 2:double missiles; 3: explosive missiles
	
    public Player() {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(player));
        width = ii.getImage().getWidth(null); 
        height = ii.getImage().getHeight(null);

        hp = 3;
        speed = 2;
        missileState = 1;
        setImage(ii.getImage());
        setX(INIT_X);
        setY(INIT_Y);
    }
    
    public void act() {
        x += dx; 
        y += dy;
        if (x <= 2) x = 2;
        if (x >= MAIN_WIDTH - width) 
            x = MAIN_WIDTH - width;
        if (y <= 2) y = 2;
        if (y >= MAIN_HEIGHT - 2*height) 
        	y = MAIN_HEIGHT - 2*height;
    }
    
    public void beingHit(int damage)
    {
    	hp -= damage;
    	if (hp <= 0) die();
    	else if (hp > 12) hp = 12;
    	
    	if (hp > 8) missileState = 4;
    	else if (hp > 5) missileState = 3; //missile upgrade
    	else if (hp > 3) missileState = 2;
    	else missileState = 1;
    }
    
    public void speedUp(int sb)
    {
    	speed += sb;
    	if (speed >= 8) //upper limit
    		speed = 8;
    	if (speed <= 2) //lower limit
    		speed = 2;
    }
    
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT)
        {
            dx = -speed;
        }

        if (key == KeyEvent.VK_RIGHT)
        {
            dx = speed;
        }
        
        if (key == KeyEvent.VK_UP)
        {
        	dy = -speed;
        }
        
        if (key == KeyEvent.VK_DOWN)
        {
        	dy = speed;
        }
        //cheat keys
        if ((e.isControlDown()) && (key == KeyEvent.VK_A)) //hit points
        {	
        	hp += 5; 
        	beingHit(0);
        }
        if ((e.isControlDown()) && (key == KeyEvent.VK_B)) //speed
        {	
        	speed +=1;	
        	if (speed >= 10) speed = 10;
        }
        if ((e.isControlDown()) && (key == KeyEvent.VK_C))
        {	
        	speed -=1;	
        	if (speed <= 1) speed = 1;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT)
        {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT)
        {
            dx = 0;
        }
        
        if (key == KeyEvent.VK_UP)
        {
        	dy = 0;
        }
        
        if (key == KeyEvent.VK_DOWN)
        {
        	dy = 0;
        }
    }
}
